package com.tiantianfresh.view.controller;

import com.tiantianfresh.model.pojo.*;
import com.tiantianfresh.view.service.DetailPageService;
import com.tiantianfresh.view.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FANGAO
 * @date 2021/11/30 - 上午 10:49
 */
@Controller
@RequestMapping("/detailPage")
public class DetailPageController {
    @Autowired
    private DetailPageService detailPageService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ResponseBody
    @RequestMapping("/queryCommodity")
    public Object queryCommodity(Integer id){
        AJAXResult result = new AJAXResult();
        CommodityResult commodityResult = detailPageService.queryCommodity(id);
        if (commodityResult!=null){
            result.setSuccess(true);
            result.setData(commodityResult);
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/queryCommodityAllocationAll")
    public Object queryCommodityAllocationAll(Integer comid){
        AJAXResult result = new AJAXResult();
        List<CommodityAllocation> commodityAllocation = detailPageService.queryCommodityAllocationAll(comid);
        if (commodityAllocation!=null){
            result.setSuccess(true);
            result.setData(commodityAllocation);
        }else{
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/insertShoppingCart")
    public Object insertShoppingCart(Integer comid,Integer quantityspqn,Integer userid){
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("comid",comid);
        map.put("quantityspqn",quantityspqn);
        map.put("userid",userid);
        result.setSuccess(shoppingCartService.insertShoppingCart(map));
        return result;
    }
    @ResponseBody
    @RequestMapping("/addOrder")
    public Object addOrder(Integer userid,Integer caid,Integer quantity){
        System.out.println();
        AJAXResult result = new AJAXResult();
        Map<String,Integer> map = new HashMap<>();
        map.put("userid",userid);
        map.put("caid",caid);
        map.put("quantity",quantity);
        Order order = detailPageService.addOrder(map);
        if (order!=null){
            result.setData(order);
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
        }
        return result;
    }
}
